﻿#region Imports
using System;
using System.Drawing;
using System.Windows.Forms;
using BulkRenamer.Util.EventArguments;
using CommonUtilities.Enums;
using CommonUtilities.Util;
#endregion

namespace BulkRenamer.Util.Controls
{
    public partial class uclOptionCheckerWin : UserControl
    {
        #region Constructors
        public uclOptionCheckerWin(bool defaultValue)
        {
            InitializeComponent();
            chkOption.CheckedChanged += new EventHandler(chkOption_CheckedChanged);
            radOptionY.CheckedChanged += new EventHandler(radOption_CheckedChanged);
            radOptionN.CheckedChanged += new EventHandler(radOption_CheckedChanged);

            chkOption.Checked = defaultValue;
            SupressAutoUpdate = false;
        }
        #endregion

        #region Enums
        public enum eEventCallInit
        {
            Pre = -1,
            ByPass = 0,
            Post = 1
        }
        #endregion

        #region Properties
        private const eEventCallInit def = eEventCallInit.Post;
        private eEventCallInit eventCallInit = def;
        public eEventCallInit EventCallInit
        {
            get { return eventCallInit; }
            private set { eventCallInit = (value == eEventCallInit.ByPass ? def : value); }
        }

        public string CheckerText
        {
            set { chkOption.Text = value; }
        }

        public bool SupressAutoUpdate { get; private set; }

        public bool CheckerValue
        {
            //get { return false; }
            //set { ; }

            get { return chkOption.Checked; }
            set { chkOption.Checked = value; }
        }

        private bool allowColours = false;
        public bool AllowColours
        {
            get { return allowColours; }
            set { allowColours = value; }
        }

        private Color colYes = Color.Green;
        public Color ColourYes
        {
            get { return colYes; }
            set { colYes = value; }
        }

        private Color colNo = Color.Red;
        public Color ColourNo
        {
            get { return colNo; }
            set { colNo = value; }
        }

        private Color colNone = Color.Transparent;
        public Color ColourNone
        {
            get { return colNone; }
            set { colNone = value; }
        }
        #endregion

        #region Event handler
        // OPTION-1: Create custom EventHandler and delegate event
        public delegate void ValueChangedEventHandler(bool sender, OptionCheckerEventArgs e);
        public event ValueChangedEventHandler ValueChanged;
        // OPTION-2: Create custom EventHandler
        //public event EventHandler ValueChanged;
        //{
        //    add { chkOption.CheckedChanged += value; }
        //    remove { chkOption.CheckedChanged -= value; }
        //}
        #endregion

        #region Events
        private void radOption_CheckedChanged(object sender, EventArgs e)
        {
            EvaluateColours();
            RadioButton rad = (RadioButton)sender;
            if (rad.Checked && !SupressAutoUpdate)
            {
                SupressAutoUpdate = true;
                chkOption.Checked = rad.Name.EndsWith("Y");
                SupressAutoUpdate = false;
            }
        }

        protected virtual void OnValueChanged(OptionCheckerEventArgs e)
        {
            if (ValueChanged != null) ValueChanged(CheckerValue, (OptionCheckerEventArgs)e);
        }

        private void chkOption_CheckedChanged(object sender, EventArgs e)
        {
            // Pre call
            if (EventCallInit == eEventCallInit.Pre) OnValueChanged((OptionCheckerEventArgs)e);
            //if (ValueChanged != null) ValueChanged(CheckerValue, (OptionCheckerEventArgs)e);

            // Bypass
            if (EventCallInit != eEventCallInit.ByPass)
            {
                EvaluateColours();
                CheckerText = chkOption.CheckState.ToString();
                if (!SupressAutoUpdate)
                {
                    SupressAutoUpdate = true;
                    if (chkOption.Checked) radOptionY.Checked = true; else radOptionN.Checked = true;
                    SupressAutoUpdate = false;
                }
            }

            // Post call
            if (EventCallInit == eEventCallInit.Post) OnValueChanged((OptionCheckerEventArgs)e);
        }

        #endregion

        #region Methods
        public void InitControls(ContentAlignment checkAlign, string optionY, string optionN)
        {
            // Content alignment
            ContentAlignment align = Utilities.GetOnlyLR(checkAlign, ContentAlignment.MiddleLeft);
            radOptionY.CheckAlign = radOptionN.CheckAlign = align;

            // Text alignment
            radOptionY.TextAlign = Utilities.InvertAlignment(radOptionY.CheckAlign, ContentAlignment.MiddleRight, eInversionMode.Horizontal);
            radOptionN.TextAlign = Utilities.InvertAlignment(radOptionN.CheckAlign, ContentAlignment.MiddleRight, eInversionMode.Horizontal);

            // Text
            radOptionY.Text = optionY;
            radOptionN.Text = optionN;
        }

        private void EvaluateColours()
        {
            this.BackColor = AllowColours ? (chkOption.Checked ? ColourYes : ColourNo) : ColourNone;
        }

        public void TogglValues()
        {
            switch (EventCallInit)
            {
                case uclOptionCheckerWin.eEventCallInit.Pre:
                    EventCallInit = uclOptionCheckerWin.eEventCallInit.Post;
                    break;
                case uclOptionCheckerWin.eEventCallInit.Post:
                    EventCallInit = uclOptionCheckerWin.eEventCallInit.Pre;
                    break;
                default:
                    break;
            }
        }

        public override string ToString()
        {
            return Utilities.ToString(this);
        }
        #endregion
    }
}
